package com.ctrlspace.model;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

public class ProjectUnit implements JSONAware {

	/** The unique identifier for the project */
	private int projectId;
	/** The ID of the owner of the project */
	private int ownerId;
	/** The title of the project */
	private String title;
	/** The description of the project */
	private String description;
	/** The nature of the project */
	private String nature;
	/** The path to the project */
	private String path;
	/** The date the project was created */
	private String dateCreated;
	/** The list of files associated with the project */
	private List<FileUnit> files = new ArrayList<FileUnit>();
	private List<String> sourcePaths = new ArrayList<String>();
	/** The options associated with the Project Unit */
	private Map<?, ?> projectOptions;
	private String iconClass;

	/**
	 * @return the projectId
	 */
	public int getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId
	 *            the projectId to set
	 */
	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}

	/**
	 * @return the ownerId
	 */
	public int getOwnerId() {
		return ownerId;
	}

	/**
	 * @param ownerId
	 *            the ownerId to set
	 */
	public void setOwnerId(int ownerId) {
		this.ownerId = ownerId;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the nature
	 */
	public String getNature() {
		return nature;
	}

	/**
	 * @param nature
	 *            the nature to set
	 */
	public void setNature(String nature) {
		this.nature = nature;
	}

	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path
	 *            the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * @return the dateCreated
	 */
	public String getDateCreated() {
		return dateCreated;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	public void setDateCreated(Date dateCreated) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.dateCreated = formatter.format(dateCreated);
	}

	/**
	 * @param files
	 *            the files to set
	 */
	public void setFiles(List<FileUnit> files) {
		this.files = files;
	}

	/**
	 * @return the files
	 */
	public List<FileUnit> getFiles() {
		return files;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	public void setDateCreated(String dateCreated) {
		this.dateCreated = dateCreated;
	}

	/**
	 * @return the projectOptions
	 */
	public Map<?, ?> getProjectOptions() {
		return projectOptions;
	}

	/**
	 * @param projectOptions
	 *            the projectOptions to set
	 */
	public void setProjectOptions(Map<?, ?> projectOptions) {
		this.projectOptions = projectOptions;
	}

	public void setIconClass(String iconClass) {
		this.iconClass = iconClass;
	}

	public String getIconClass() {
		return iconClass;
	}

	public boolean addSourcePath(String e) {
		return sourcePaths.add(e);
	}

	public boolean removeSourcePath(Object o) {
		return sourcePaths.remove(o);
	}

	public List<String> getSourcePaths() {
		return sourcePaths;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		Map out = new LinkedHashMap();
		out.put("id", projectId);
		out.put("ownerId", ownerId);
		out.put("label", title);
		out.put("description", description);
		out.put("nature", nature);
		out.put("dateCreated", dateCreated.toString());
		out.put("iconClass", "csIcons " + getIconClass());
		out.put("children", files);

		return JSONObject.toJSONString(out);
	}
}